package service.impl;

import dao.JxbZzMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.JxbZz;
import service.JxbZzService;

import java.util.List;

/**
 * 疾病症状关联表业务逻辑实现类
 */
@Service
public class JxbZzServiceImpl implements JxbZzService {
    @Autowired
    private JxbZzMapper jxbZzMapper;

    @Override
    public List<JxbZz> findByJxbId(Integer jxbId) {
        return jxbZzMapper.findByJxbId(jxbId);
    }

    @Override
    public List<JxbZz> findByZzId(Integer zzId) {
        return jxbZzMapper.findByZzId(zzId);
    }

    @Override
    public JxbZz findByJxbAndZz(Integer jxbId, Integer zzId) {
        return jxbZzMapper.findByJxbAndZz(jxbId, zzId);
    }

    @Override
    public int batchAdd(List<JxbZz> jxbZzList) {
        return jxbZzMapper.batchAdd(jxbZzList);
    }

    @Override
    public int add(JxbZz jxbZz) {
        // 验证权重范围
        if (jxbZz.getWeight() < 1 || jxbZz.getWeight() > 5) {
            throw new IllegalArgumentException("权重必须在1-5之间");
        }
        // 检查关联是否已存在
        JxbZz existing = findByJxbAndZz(jxbZz.getJxbId(), jxbZz.getZzId());
        if (existing != null) {
            throw new IllegalStateException("该疾病与症状的关联已存在");
        }
        return jxbZzMapper.add(jxbZz);
    }

    @Override
    public int updateWeight(JxbZz jxbZz) {
        // 验证权重范围
        if (jxbZz.getWeight() < 1 || jxbZz.getWeight() > 5) {
            throw new IllegalArgumentException("权重必须在1-5之间");
        }
        return jxbZzMapper.updateWeight(jxbZz);
    }

    @Override
    public int deleteByJxbId(Integer jxbId) {
        return jxbZzMapper.deleteByJxbId(jxbId);
    }

    @Override
    public int deleteByZzId(Integer zzId) {
        return jxbZzMapper.deleteByZzId(zzId);
    }

    @Override
    public int deleteByJxbAndZz(Integer jxbId, Integer zzId) {
        return jxbZzMapper.deleteByJxbAndZz(jxbId, zzId);
    }
}